package com.epfresh.supplier.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.epfresh.api.constant.AppSupplyRouterConstant;
import com.epfresh.api.entity.RequestEntitySupply;
import com.epfresh.api.entity.ResponseEntity;
import com.epfresh.api.entity.StatusEntity;
import com.epfresh.api.http.OnRequestListener;
import com.epfresh.api.utils.InputControl;
import com.epfresh.api.utils.VerificationUtils;
import com.epfresh.supplier.R;
import com.epfresh.api.global.BaseActivity;
import com.epfresh.api.utils.T;
import com.epfresh.api.utils.ToastUtils;
import com.google.gson.Gson;

import java.util.HashMap;
import java.util.Map;

public class EditNicknameActivity extends BaseActivity {
    static final String TAG = "EditNicknameActivity";
    EditText nicknameET;
    TextView sure;
    String nickname = "";
    String id = "";
    //String token = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_editnickname);
        ivBack.setVisibility(View.VISIBLE);
        toolbarTitle.setText("更改姓名/名称");
        nicknameET = (EditText) findViewById(R.id.nickname);
        sure = (TextView) findViewById(R.id.sure);
        sure.setOnClickListener(this);
        nickname = getUser().getNickname();
        id = getUser().getAccountId();
        //token = getUser().getToken();
        nicknameET.setText(nickname);
        if (nickname != null)
            nicknameET.setSelection(nicknameET.getText().length());
        rightbtn.setText("提交");
        rightbtn.setOnClickListener(this);
        rightbtn.setVisibility(View.VISIBLE);
    }

    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.baseback:
                finish();
                break;
            case R.id.rightbtn:
                if (InputControl.isEmpty(nicknameET)) {
                    ToastUtils.TextToast(this, "姓名/名称不可为空！");
                    return;
                }
                if (!VerificationUtils.isValid(nicknameET.getText().toString())) {
                    toast(getString(R.string.illegal_input, "姓名/名称"));
                    return;
                }
                nickname = nicknameET.getText().toString();
                mHttpUp();
                break;
        }
    }

    /**
     * 提交修改
     */
    private void mHttpUp() {

        RequestEntitySupply<Map<String, String>> data = new RequestEntitySupply<>();

        data.setCmd(AppSupplyRouterConstant.cmd_account_account_update);
        data.setClientIdentifierCode(getAppApplication().getClientIdentifierCode());
        Map<String, String> map = new HashMap<>();
        //data.setToken(getUser().getToken());
        map.put("id", getUser().getAccountId());
        map.put("nickname", nickname);
        data.setParameters(map);
        request(data, AppSupplyRouterConstant.cmd_account_account_update, onRequestListener);
    }

    OnRequestListener<StatusEntity> onRequestListener = new OnRequestListener<StatusEntity>() {
        @Override
        public StatusEntity jsonToObj(String responseStr) {
            return new Gson().fromJson(responseStr, StatusEntity.class);
        }

        @Override
        public void onFail(int failCode, Object arg, Object tag) {


        }

        @Override
        public void onResponseError(int failCode, Object arg, Object tag) {
        }

        @Override
        public void onStart(Object tag) {

        }

        @Override
        public void onResponse(ResponseEntity<StatusEntity> response, Object tag) {
            StatusEntity responseElement = response.getResponseElement();
            if (responseElement.isSuccess()) {
                getUser().setNickname(nickname);
                finish();
                return;
            }
            T.toast("修改出现异常，请稍后再试");
        }
    };

}
